<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/20
 * Time: 19:30
 */
namespace common\logics\taobao;

use common\widgets\Request;
use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;

class TaoBaoLogic extends BaseObject implements ITaoBao
{
    /** @var \common\sources\write\taobao\TaoBaoAccess $WTaoBaoAccess */
    private $WTaoBaoAccess;
    /** @var \common\sources\read\taobao\TaoBaoAccess $RTaoBaoAccess */
    private $RTaoBaoAccess;

    /** @var  \common\sources\read\message\MesgAccess  $RMesgAccess */
    private $RMesgAccess;
    /** @var \common\sources\write\message\MesgAccess  $WMesgAcdess */
    private $WMesgAcdess;

    public function init()
    {
        $this->WTaoBaoAccess = Yii::$container->get('WTaoBaoAccess');
        $this->RTaoBaoAccess = Yii::$container->get("RTaoBaoAccess");
        $this->RMesgAccess = Yii::$container->get('RMesgAccess');
        $this->WMesgAcdess = Yii::$container->get('WMesgAccess');
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function addTaoBao($param)
    {
        if(empty($param['tbname'])){
            return Tool::returnJson([],401,'淘宝天猫会员名不能为空');
        }
        if(empty($param['buy_id'])){
            return Tool::returnJson([],402,'买手用户id不能为空');
        }
        if(empty($param['sex'])){
            return Tool::returnJson([],403,'性别不能为空');
        }
        if(empty($param['zfbname'])){
            return Tool::returnJson([],405,'支付宝姓名不能为空');
        }
        if(empty($param['zfbage'])){
            return Tool::returnJson([],406,'支付宝年龄不能为空');
        }
        if(empty($param['takename'])){
            return Tool::returnJson([],407,'收货姓名不能为空');
        }
        if(empty($param['takephone'])){
            return Tool::returnJson([],408,'收货手机号不能为空');
        }
        if(empty($param['pro_name'])){
            return Tool::returnJson([],409,'省名称不能为空');
        }
        if(empty($param['pro_code'])){
            return Tool::returnJson([],410,'省代号不能为空');
        }
        if(empty($param['city_name'])){
            return Tool::returnJson([],411,'市名称不能为空');
        }
        if(empty($param['city_code'])){
            return Tool::returnJson([],412,'市代号不能为空');
        }
        if(empty($param['area_name'])){
            return Tool::returnJson([],413,'地区名称不能为空');
        }
        if(empty($param['area_code'])){
            return Tool::returnJson([],415,'地区代号不能为空');
        }
        if(empty($param['addr'])){
            return Tool::returnJson([],416,'街道地址不能为空');
        }
        if(empty($param['zfbname_img'])){
            return Tool::returnJson([],417,'支付宝实名认证截图不能为空');
        }
        if(empty($param['ip_img'])){
            return Tool::returnJson([],418,'ip截图不能为空');
        }
        if(empty($param['tqz_img'])){
            return Tool::returnJson([],419,'淘气值截图不能为空');
        }
        if(empty($param['takeaddr'])){
            return Tool::returnJson([],420,'收货地址截图不能为空');
        }
        if(empty($param['zfbhb_img'])){
            return Tool::returnJson([],421,'收货地址截图不能为空');
        }
        if(empty($param['sms_code'])){
            return Tool::returnJson([],422,'短信验证码不能为空');
        }
        //1. 收货地址短信验证码 校验
        $smsArr = $this->RMesgAccess->getVerifyCode($param['takephone'],6);
        if($smsArr['verifycode'] != $param['sms_code']){
            return Tool::returnJson([],423,'短信验证码不正确');
        }
        //查看用户绑定买号个数限制
        $count = $this->RTaoBaoAccess->getCount($param['buy_id']);
        if($count>2){
            return Tool::returnJson([],427,'买号绑定已经达上限');
        }
        //正式逻辑代码
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $param['create_time'] = time();
            $param['audit_status'] = 1;
            $result = $this->WTaoBaoAccess->addTaoBao($param);
            if(!$result){
                $transaction->rollBack();
                return Tool::returnJson([],425,'添加淘宝天猫失败');
            }
            $this->WMesgAcdess->modifyCodeStatus($smsArr['id']);
            $transaction->commit();
            return Tool::returnJson([],200,'添加淘宝天猫会员成功');

        }catch (\Exception $e){
            $transaction->rollBack();
            return Tool::returnJson([],426,'添加淘宝天猫失败');
        };

    }
    //获取绑定淘宝天猫列表
    public function getList($data, $param)
    {
        if(empty($data['buy_id'])){
            return Tool::returnJson([],401,'买手用户id不能为空');
        }
        $result = $this->RTaoBaoAccess->getList($data["buy_id"]);
        if(count($result))
        {
            foreach ($result as &$res){
                $res['audit_status'] = isset($param['audit_status'][$res['audit_status']]) ? $param['audit_status'][$res['audit_status']] :'待审核';
            }
        }
        return Tool::returnJson($result,200,"成功");
    }

}